//回去上级选择列表
Load()
var area_detail={}
//{
//	where_json:JSON.stringify({
//		area_id:['in',{}]
//	})
//}

ajax(api.area.list,{
	is_all:1
},'post').done(function(res){
	Load(1)
	if(res.code==1){
	
	var tree=[];
	tree=fortree(tree,res.data,1)
	function fortree(tree,data,type){
		for(var i=0;i<data.length;i++){
			var qz=''
			for(var ss=1;ss<type;ss++){
				qz+='├'
			}
			data[i].area_name2=qz+data[i].area_name
			tree.push(data[i])
			if(data[i].children.length>0){
				fortree(tree,data[i].children,type+1)
			}else{}
		}
		return tree
	}
	console.log(tree)
	var options=`<option value='0'>顶级</option>`
	{if $Think.get.area_id}
	for(var didx=0;didx<tree.length;didx++){
		if(tree[didx].area_id=='{$Think.get.area_id}'){
			area_detail=tree[didx]
		}
		
	}
	console.log(area_detail)
	$('input').each(function(){
		$(this).val(area_detail[$(this).attr('name')])
	})
	{/if}
	for(var i=0;i<tree.length;i++){
		{if $Think.get.area_id}
			if(tree[i].area_id==area_detail.area_pid){
				options+=`<option selected value='${tree[i].area_id}'>${tree[i].area_name2}</option>`
			}else{
				options+=`<option value='${tree[i].area_id}'>${tree[i].area_name2}</option>`
			}
		{else/}
		options+=`<option value='${tree[i].area_id}'>${tree[i].area_name2}</option>`
		{/if}
		
	}
	$('.area_pid').html(options)
	form.render('select');
	{if $Think.get.area_id}
		
	{else/}
	
	{/if}
	
	
	}else{
		Err(res.msg)
	}
}).fail(function(){
	Load(1)
	Err('请求失败')
})


form.on('submit(ss1)', function(data){
	Load()
	   //当前容器的全部表单字段，名值对形式：{name: value}
	  data.field.module_type=0
	  console.log(data.field)
	  {if $Think.get.area_id}
	  sumbit=api.area.edit
	  data.field.area_id="{$Think.get.area_id}"
	  {else/}
	  sumbit=api.area.add
	  {/if}
	 	ajax(sumbit,data.field,'post').done(function(res) {
	 		Load(1)
	 		Msg("添加成功")
			console.log(res)
			{if $Think.get.area_id}
				window.history.go(0)
			{else/}
				window.location.href='{:url("area")}'
			{/if}
		}).fail(function(err) {
			Load(1)
			Err('请求失败')
		});

	  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
	});